package com.booklove.controller.user;


import com.booklove.constant.JwtClaimsConstant;
import com.booklove.domain.dto.PageDTO;
import com.booklove.domain.dto.UserLoginDTO;
import com.booklove.domain.dto.UserLoginVO;
import com.booklove.domain.po.User;
import com.booklove.domain.query.UserQuery;
import com.booklove.properties.JwtProperties;
import com.booklove.result.Result;
import com.booklove.service.user.UserService;
import com.booklove.utils.JwtUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;


@Slf4j
@RestController("userCLL")
@RequestMapping("/users")
@RequiredArgsConstructor// 使用基本注解的注入形式，仅仅注入需要的变量
public class UserController {

    private final UserService userService;
    private final JwtProperties jwtProperties;

    /**
     * 保存新增
     */
    @PostMapping("/")
    Result<String> saveUser(@RequestBody User userFormDTO) {
        User user = new User();
        BeanUtils.copyProperties(userFormDTO, user);
        userService.save(user);
        return Result.success();
    }


    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/id")
    Result<String> deleteUser(@RequestParam("id") Long id) {
        userService.removeById(id);
        return Result.success();
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    Result<User> getUser(@PathVariable Long id) {
        return Result.success(userService.getById(id));
    }


    /**
     * 编辑数据
     *
     * @param userFormDTO 实体
     * @return 编辑结果
     */
    @PutMapping("/")
    Result<String> updateUser(@RequestBody User userFormDTO) {
        userService.updateById(userFormDTO);
        return Result.success();
    }


    /**
     * 分页查询
     *
     * @param userQuery 筛选条件
     * @return 查询结果
     */
    @GetMapping("/page")
    PageDTO<User> usersPageQuery(UserQuery userQuery) {
        // 分页限制
        if(userQuery.getPageNo()==null || userQuery.getPageSize()==null){
            userQuery.setPageNo(1);
            userQuery.setPageSize(10); // 默认pageSize大小
        }
        return userService.queryUsersPage(userQuery);
    }


    /**
     * 微信login
     * @param userLoginDTO
     * @return
     */
    @PostMapping("/wxlogin")
    public Result<UserLoginVO> openIdLogin(@RequestBody UserLoginDTO userLoginDTO) {

        log.info("微信用户进行登录{}", userLoginDTO);
        User user = userService.wxLogin(userLoginDTO);
        // 为登录的微信用户生成jwt的令牌（公共操作）
        HashMap<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID, user.getId());
        String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), claims);

        //封装准备返回的用户信息
        UserLoginVO userLoginVO = UserLoginVO.builder()
                .id(user.getId())
                .username(user.getUsername())
                .openid(user.getOpenid())
                .authorization(token)
                .build();

        // 返回含有token的显示段落
        return Result.success(userLoginVO);
    }

}

